package tptecnicas.configuration;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import tptecnicas.util.Consts;

/*
 * PropertiesReader se encarga de parsear el archivo de configuracion .properties para luego
 * devolver una instancia de Configuration. 
 */

public class PropertiesReader {
	private Properties properties;
	private Configuration configuration;
	
	public PropertiesReader() {
		properties = new Properties();
		configuration = new Configuration();
	}

	public Configuration readFromProperties(InputStream is) {
		try {
			properties.load(is);
		} catch (IOException e) {
			System.err.println("Caught IOException: " +  e.getMessage());
		}
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
				System.err.println("Caught IOException: " +  e.getMessage());
			}
		}
		
		readTerminalConfig();
		readFileConfig();
		readCustomConfig();
		readSeparator();
		return configuration;
	}
	
	public Configuration readFromDefaultProperties() {
		properties.setProperty("TerminalLevel", "OFF");
		properties.setProperty("TerminalFormat", Consts.DEFAULT_FORMAT);
		properties.setProperty("LogFile1", Consts.DEFAULT_LOGPATH);
		properties.setProperty("FileLevel1", "TRACE");
		properties.setProperty("FileFormat1", Consts.DEFAULT_FORMAT);
		properties.setProperty("LogCustom1", Consts.DEFAULT_CUSTOMPATH);
		properties.setProperty("CustomLevel1", "DEBUG");
		properties.setProperty("CustomFormat1", Consts.DEFAULT_FORMAT);
		properties.setProperty("separator", ",");
		
		readTerminalConfig();
		readFileConfig();
		readCustomConfig();
		readSeparator();
		return configuration;
	}

	private void readTerminalConfig() {
		String level = properties.getProperty("TerminalLevel", "OFF");
		String format = properties.getProperty("TerminalFormat", Consts.DEFAULT_FORMAT);

		LogConfig config = new LogConfig("Terminal", level, format);
		configuration.addLogConfig(config);
	}
	
	private void readFileConfig() {
		int n = getQuantity("LogFile");

		for (int i = 1; i <= n; i++) {
			String path = properties.getProperty("LogFile" + Integer.toString(i),
					"src/log/log" + Integer.toString(i) + ".txt");
			String level = properties.getProperty("FileLevel" + Integer.toString(i), "OFF");
			String format = properties.getProperty("FileFormat" + Integer.toString(i), Consts.DEFAULT_FORMAT);
			configuration.addLogConfig(new LogConfig(path, level, format));
		}
	}
	
	private void readCustomConfig() {
		int n = getQuantity("LogCustom");
		
		for (int i = 1; i <= n; i++) {
			String path = properties.getProperty("LogCustom" + Integer.toString(i));
			String level = properties.getProperty("CustomLevel" + Integer.toString(i), "OFF");
			String format = properties.getProperty("CustomFormat" + Integer.toString(i), Consts.DEFAULT_FORMAT);
			configuration.addLogConfig(new LogConfig(path, level, format));
		}
	}
	
	private int getQuantity(String dest) {
		int n = 0;
		Enumeration e = properties.keys();
		while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			if (key.contains(dest)) {
				n++;
			}
		}
		return n;
	}
	
	private void readSeparator() {
		configuration.setSeparator(properties.getProperty("separator"));
	}

}
